﻿#include "progressmonitorthread.h"
#include <QDebug>
#include <QFileInfo>

ProgressMonitorThread::ProgressMonitorThread(QObject *parent) : QThread(parent)
{
    moveToThread(this);
    isRun = false;
    start();
}

ProgressMonitorThread::~ProgressMonitorThread()
{
    qDebug() << __FUNCTION__;
    stop();
    quit();
    wait();
}

void ProgressMonitorThread::stop()
{
   isRun = false;
}

void ProgressMonitorThread::run()
{
    isRun = true;
    progressManage.enableDebugPriv();
    QList<Progress> progressList;
    QList<ProgressInfo> progressInfoList;
    while (isRun){
        progressList.clear();
        progressInfoList.clear();
        if(!progressService.getList(progressList)){
            for(int sleep_time = 500; sleep_time > 0 && isRun; sleep_time --){
                QThread::msleep(1);
            }
            qCritical() << "获取进程列表失败！";
            continue;
        }
        for(int i=0;i<progressList.size();i++){
            uint64_t progressInfoId = progressList.at(i).getId();
            if(!progressInfoMap.contains(progressInfoId)){
               progressInfoMap[progressInfoId]  = ProgressInfo();
            }
            progressInfoMap[progressInfoId].setProgress(progressList.at(i));
            if(!QFileInfo::exists(QString(progressList.at(i).getPath()))){
                progressInfoMap[progressInfoId].setStateMsg(QString("路径错误"));
            }else{ //进程路径存在
                string progressPath = QString(progressList.at(i).getPath()).toStdString();
                DWORD progressId;
                if(progressManage.getProcessIdByPath(progressPath,progressId)){ //获取进程ID成功
                    progressInfoMap[progressInfoId].setEnabled(true);

                    uint64_t runTime;//运行时长（s）
                    float cpuUsage;//cpu占用率
                    uint64_t memory;//内存
                    uint64_t virtualMemory;//虚拟内存
                    uint64_t ioRead;//io读
                    uint64_t ioWrite;//io写

                    progressManage.get_cpu_usage(progressId,cpuUsage);
                    progressManage.get_memory_usage(progressId,memory, virtualMemory);
                    progressManage.get_io_bytes(progressId,ioRead,ioWrite);
                    progressManage.getProcessTimeById(progressId,runTime);

                    progressInfoMap[progressInfoId].setCpuUsage(cpuUsage);
                    progressInfoMap[progressInfoId].setMemory(memory);
                    progressInfoMap[progressInfoId].setVirtualMemory(virtualMemory);
                    progressInfoMap[progressInfoId].setIoRead(ioRead);
                    progressInfoMap[progressInfoId].setIoWrite(ioWrite);
                    progressInfoMap[progressInfoId].setRunTime(runTime);
                }else{
                    progressInfoMap[progressInfoId].setEnabled(false);
                    progressInfoMap[progressInfoId].setCpuUsage(0);
                    progressInfoMap[progressInfoId].setMemory(0);
                    progressInfoMap[progressInfoId].setVirtualMemory(0);
                    progressInfoMap[progressInfoId].setIoRead(0);
                    progressInfoMap[progressInfoId].setIoWrite(0);
                    progressInfoMap[progressInfoId].setRunTime(0);
                    if(!progressList.at(i).getDaemon_open()){
                        progressInfoMap[progressInfoId].setStateMsg(QString("守护关闭"));
                    }
                }
            }
//            qDebug() << progressInfoMap[progressInfoId].getCpuUsageList().size() << progressInfoMap[progressInfoId].toJsonObject();
            progressInfoList.append(progressInfoMap[progressInfoId]);
            QThread::msleep(1);
        }
        emit signal_progressInfoList(progressInfoList);
        for(int sleep_time = 100; sleep_time > 0 && isRun; sleep_time --){
            QThread::msleep(10);
        }
    }
}
